home *** CD-ROM | disk | FTP | other *** search
/ AppleScript - The Beta Release / AppleScript - The Beta Release.iso / Documentation / develop / Apple Event Objects and You / Apple Event Objects (code) / AppleEventHandlers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-08  |  5.7 KB  |  152 lines  |  [TEXT/KAHL]

  1. /* File: AppleEventHandlers.c */
  2. #include "AEObjects.h"
  3. #include "AEPackObject.h"
  4. #include "AERegistry.h"
  5. #include "ScriptScrap.h"
  6. #include "Prototypes.h"
  7.  
  8.  
  9. void InstallAppleEventHandlers(void)
  10. {
  11.     OSErr    err;
  12.     long    result;
  13.     
  14.     err = Gestalt(gestaltAppleEventsAttr, &result);
  15.     if ((err == noErr) && (result & 1)) {
  16.         err = AEObjectInit();
  17.         
  18.         (void)AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, (EventHandlerProcPtr)HandleOapp, 0, false);
  19.         (void)AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,   (EventHandlerProcPtr)HandleOdoc, 0, false);
  20.         (void)AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,  (EventHandlerProcPtr)HandlePdoc, 0, false);
  21.         (void)AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, (EventHandlerProcPtr)HandleQuit, 0, false);
  22.         
  23.         /* This is a "catch-all" for debugging -- it reports if an event wasn't handled */
  24.         (void)AEInstallEventHandler(typeWildCard,    typeWildCard,         (EventHandlerProcPtr)NotHandled, 0, false);
  25.  
  26.         /* Install our event handlers for Object Model Apple Events */
  27.         (void)AEInstallEventHandler(kAECoreSuite,     typeWildCard,          (EventHandlerProcPtr)DirectParamDispatch, 0, false);
  28.         (void)AEInstallEventHandler(kAECoreSuite,     kAECreateElement,     (EventHandlerProcPtr)CreateElementDispatch, 0, false);
  29.         /* Install the object accessors for use with the Object Support Library */
  30.         (void)AEInstallObjectAccessor('prop', typeNull, (accessorProcPtr)PropertyFromNullAccessor, 0, false);
  31.  
  32.         (void)AEInstallObjectAccessor(cWindow, typeNull, (accessorProcPtr)WindowFromNullAccessor, 0, false);
  33.         (void)AEInstallObjectAccessor(cDocument, typeNull, (accessorProcPtr)WindowFromNullAccessor, 0, false);
  34.         (void)AEInstallObjectAccessor(typeProperty, cDocument, (accessorProcPtr)PropertyFromWindowAccessor, 0, false);
  35.         (void)AEInstallObjectAccessor(typeProperty, typeNull, (accessorProcPtr)PropertyFromNullAccessor, 0, false);
  36.         
  37.         (void)AEInstallObjectAccessor(cEntry, cDocument, (accessorProcPtr)EntryFromDocumentAccessor, 0, false);
  38.         (void)AEInstallObjectAccessor(cScrapItem, cEntry, (accessorProcPtr)ItemFromEntryAccessor, 0, false);
  39.         (void)AEInstallObjectAccessor(typeProperty, cEntry, (accessorProcPtr)PropertyFromEntryAccessor, 0, false);
  40.         (void)AEInstallObjectAccessor(typeProperty, cScrapItem, (accessorProcPtr)PropertyFromItemAccessor, 0, false);
  41.     }
  42. } /* init_apple_events_hook */
  43.  
  44.  
  45. char    *OSTypeToPStr(OSType theType, char *s)
  46. /* This is a useful debugging tool as it allows us to put an EventID or EventID */
  47. /* into a string so we can display it in a dialog */
  48. {
  49.     *s = 0x04;
  50.     BlockMove(&theType, &s[1], 4);    /* Use BlockMove to avoid odd address error on 68000 machines */
  51.     return s;
  52. }
  53.  
  54. pascal OSErr NotHandled (AEDescList *aevt, AEDescList *reply, long refCon)
  55. /* This routine is called for any unknown events that come in (it's installed into the */
  56. /* table with wild-card handlers) and it will put up a dialog that lets us know that a */
  57. /* "bad" Apple event has come in. */
  58. {
  59.     OSErr    err;
  60.     OSType    eventClass, eventID;
  61.     OSType    typeCode;
  62.     char    scratchClass[5], scratchID[5];
  63.     Size    actualSize;
  64.     short    itemHit;
  65.     
  66.     AEDesc    directParam;
  67.     
  68.  
  69.     err = MyInteractWithUser(TRUE);    /* Tell the user it's urgent */
  70.     if (err == noErr) {
  71.         AEGetAttributePtr(aevt, keyEventClassAttr, typeType, &typeCode, (Ptr)&eventClass, sizeof(eventClass), &actualSize);
  72.         AEGetAttributePtr(aevt, keyEventIDAttr, typeType, &typeCode, (Ptr)&eventID,    sizeof(eventID),    &actualSize);
  73.         ParamText((void *)OSTypeToPStr(eventClass, scratchClass), (void *)OSTypeToPStr(eventID, scratchID), (void *)"", (void *)"");
  74.         itemHit = Alert(1002, 0L);
  75.     }
  76.     return errAEEventNotHandled;
  77. } /* NotHandled */
  78.  
  79.  
  80. pascal OSErr HandleOapp (AEDescList *aevt, AEDescList *reply, long refCon)
  81. {
  82.     /* Find the standard Scrapbook File and open it */
  83.     /* It's called "Scrapbook file" and is located in the System folder */
  84.     
  85.     OSErr    err;
  86.     FSSpec    scrapbookFile;
  87.     Str63    fileName = "\pScrapbook File";
  88.  
  89.     /* Locate the system folder */
  90.     err = FindFolder(kOnSystemDisk,kSystemFolderType,false, &scrapbookFile.vRefNum, &scrapbookFile.parID);
  91.     if (err == noErr) {
  92.         /* Set the file's name to "Scrapbook file" */
  93.         BlockMove(fileName, &scrapbookFile.name, fileName[0] + 1);
  94.         /* Open a window containing this file */
  95.         err = NewDisplayWindow(&scrapbookFile);
  96.     }    
  97.     return err;
  98. } /* NotHandled */
  99.  
  100.  
  101. pascal OSErr HandleOdoc (AEDescList *aevt, AEDescList *reply, long refCon)
  102. {
  103.     AEDesc        fileListDesc;
  104.     long        numFiles;
  105.     DescType    actualType;
  106.     long        actualSize;
  107.     AEKeyword    actualKeyword;
  108.     FSSpec        oneFile;
  109.     long        index;
  110.     OSErr        err;
  111.                         
  112.     /* The "odoc" and "pdoc" messages contain a list of aliases as the direct paramater.  */
  113.     /* This means that we'll need to extract the list, count the list's elements, and     */
  114.     /* then process each file in turn.                                                                                                         */
  115.     
  116.     /* Extract the list of aliases into fileListDesc */
  117.     err = AEGetKeyDesc( aevt, keyDirectObject, typeAEList, &fileListDesc );
  118.     if (err) return err;
  119.         
  120.     /* Count the list elements */
  121.     err = AECountItems( &fileListDesc, &numFiles);
  122.     if (err) return err;
  123.                 
  124.     /* now get each file from the list and process it. */
  125.     /* Even though the event contains a list of alises, the Apple Event Manager */
  126.     /* will convert each alias to an FSSpec if we ask it to. */
  127.     for (index = 1; index <= numFiles; index ++) {
  128.         err = AEGetNthPtr( &fileListDesc, index, typeFSS, &actualKeyword,
  129.                             &actualType, (Ptr)&oneFile, sizeof(oneFile), &actualSize);
  130.         if (err) {
  131.             AEDisposeDesc(&fileListDesc );
  132.             return err;
  133.         }
  134.         
  135.         NewDisplayWindow(&oneFile);
  136.     }
  137.     return noErr;
  138. } /* HandleOdoc */
  139.  
  140.  
  141. pascal OSErr HandlePdoc (AEDescList *aevt, AEDescList *reply, long refCon)
  142. {
  143.     return errAEEventNotHandled;    /* We don't do printing… */
  144. } /* HandlePdoc */
  145.  
  146.  
  147. pascal OSErr HandleQuit (AEDescList *aevt, AEDescList *reply, long refCon)
  148. {
  149.     gDone = TRUE;
  150.     return noErr;
  151. } /* NotHandled */
  152.